package com.czmh.etc.onlinepubsys.queue;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czmh.etc.onlinepubsys.model.EtcPayOrder;
import com.czmh.etc.onlinepubsys.services.EtcPayOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 监听器-自动取消超时没有支付订单
 * <p>
 * Created by eric on 2021-06-11.
 */
@Slf4j
@Component
public class AutoCancelTimeOutOrderListener implements RedisDelayedQueueListener<String> {

    @Autowired

    EtcPayOrderService omsOrderService;


    @Override
    public void invoke(String orderId) {
        //查询待付款订单
        QueryWrapper<EtcPayOrder> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<EtcPayOrder> lambda = wrapper.lambda();
        lambda.eq(EtcPayOrder::getId, orderId);
        lambda.eq(EtcPayOrder::getPayState, 0);
        lambda.eq(EtcPayOrder::getType, 2);
        lambda.eq(EtcPayOrder::getDeleteFlag, 0);
        int count = omsOrderService.count(wrapper);
        if (count != 1) {
            log.info("订单状态不满足自动取消条件:{}", JSON.toJSONString(omsOrderService.getById(orderId)));
        } else {
            EtcPayOrder beforeOrder = omsOrderService.getById(orderId);
            log.info("开始定时任务-自动取消订单-beforeOrder:{}", JSON.toJSONString(beforeOrder));
            beforeOrder.setPayState(9);
            beforeOrder.setUpdatedTime(new Date());
            omsOrderService.updateById(beforeOrder);
            //打印订单信息
            EtcPayOrder afterOrder = omsOrderService.getById(orderId);
            log.info("结束定时任务-自动取消订单-afterOrder:{}", JSON.toJSONString(afterOrder));
        }
    }
}
